//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.views.controller;

import ciicos.base.ActionResult;
import ciicos.base.NoDataSourceBind;
import ciicos.base.util.OptimizeUtil;
import ciicos.config.ConfigValueUtil;
import ciicos.constant.MsgCode;
import ciicos.model.FileListVO;
import ciicos.model.ImageVO;
import ciicos.util.FileDownloadUtil;
import ciicos.util.FilePathUtil;
import ciicos.util.FileUploadUtils;
import ciicos.util.StringUtil;
import ciicos.util.UpUtil;
import ciicos.util.context.SpringContext;
import ciicos.views.model.viewshmi.ViewsHmiInfoVO;
import ciicos.views.model.viewsproject.ViewsProjectVO;
import cn.xuyanwu.spring.file.storage.FileInfo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(
    tags = {"工程管理"},
    value = "viewsManage"
)
@RequestMapping({"/api/viewsManage/ViewsProject"})
public class ViewsProjectController {
    private static final Logger log = LoggerFactory.getLogger(ViewsProjectController.class);
    @Autowired
    private ConfigValueUtil configValueUtil;

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<ViewsProjectVO> detailInfo(@PathVariable("id") String id) {
        ViewsProjectVO vo = new ViewsProjectVO();
        ActionResult<ViewsHmiInfoVO> viewsHmiInfoVOActionResult = ((ViewsHmiController)SpringContext.getBean(ViewsHmiController.class)).detailInfo(id);
        if (viewsHmiInfoVOActionResult.getCode() != 200) {
            return ActionResult.fail("viwes详情获取失败");
        } else {
            List<ViewsHmiInfoVO> views = new ArrayList();
            views.add(viewsHmiInfoVOActionResult.getData());
            Map<String, List<ViewsHmiInfoVO>> hmi = new HashMap();
            hmi.put("views", views);
            vo.setHmi(hmi);
            vo.setVersion("1.01");
            vo.setScripts(new JSONArray());
            vo.setTexts(new JSONArray());
            vo.setReports(new JSONArray());
            vo.setAlarms(new JSONArray());
            vo.setPlugin(new JSONArray());
            vo.setGraphs(new JSONArray());
            vo.setCharts(new JSONArray());
            vo.setNotifications(new JSONArray());
            vo.setServer(JSONObject.parse("{\"id\":\"0\",\"name\":\"WebSCADA\",\"type\":\"SCADAServer\",\"enabled\":true,\"property\":{},\"polling\":1000,\"tags\":{}}"));
            vo.setDevices(JSONObject.parse("{\"0\":{\"id\":\"0\",\"name\":\"System Server\",\"type\":\"iViewRestAPI\",\"property\":{\"port\":null,\"slot\":null,\"rack\":null,\"baudrate\":9600,\"databits\":8,\"stopbits\":1,\"parity\":\"None\"},\"enabled\":true,\"tags\":{},\"polling\":1000}}"));
            return ActionResult.success(vo);
        }
    }

    @GetMapping({"/settings"})
    public ActionResult settings() {
        return ActionResult.success();
    }

    @ApiOperation("获取webscada图片目录")
    @GetMapping({"/imageList"})
    public ActionResult<List<FileListVO>> list(@RequestParam(name = "path",required = false) String path) {
        String filePath = this.configValueUtil.getWebScadaImagesPath();
        if (StringUtil.isNotEmpty(path)) {
            filePath = filePath + path + "/";
        }

        List<FileListVO> fileList = FileUploadUtils.getFileList(filePath);
        List<FileListVO> sortedList = (List)fileList.stream().map((fileListVO) -> {
            String[] split = fileListVO.getFileName().split("/");
            fileListVO.setFileName(split[split.length - 1]);
            return fileListVO;
        }).sorted(Comparator.comparing(FileListVO::getFileName)).collect(Collectors.toList());
        return ActionResult.success(sortedList);
    }

    @ApiOperation("获取图片")
    @GetMapping({"/imageDownload"})
    public void downLoadImg(@RequestParam("path") String path, @RequestParam("fileName") String fileName) {
        String filePath = FilePathUtil.getFilePath("webscadaimages/" + path + "/");
        byte[] bytes = FileUploadUtils.downloadFileByte(filePath, fileName, false);
        FileDownloadUtil.downloadFile(bytes, fileName, (String)null);
    }

    @NoDataSourceBind
    @Operation(
        summary = "上传文件"
    )
    @PostMapping(
        value = {"/imageUpload/{path}"},
        consumes = {"multipart/form-data"}
    )
    public ActionResult<ImageVO> file(@RequestParam("file") MultipartFile file, @PathVariable("path") String path) {
        String fileType = UpUtil.getFileType(file);
        if (!OptimizeUtil.fileType(this.configValueUtil.getAllowUploadImageType(), fileType)) {
            return ActionResult.fail(MsgCode.FA017.get() + "。允许上传格式为：" + this.configValueUtil.getAllowUploadImageType());
        } else {
            ImageVO vo = new ImageVO();
            if (path != null) {
                String filePath = this.configValueUtil.getWebScadaImagesPath() + path + "/";
                String name = file.getOriginalFilename();
                FileInfo fileInfo = FileUploadUtils.uploadFile(file, filePath, name);
                vo.setOriginalName(fileInfo.getOriginalFilename());
                vo.setName(fileInfo.getFilename());
                vo.setLink("/api/viewsManage/ViewsProject/imageDownload?path=" + path + "&fileName=" + fileInfo.getFilename());
            }

            return ActionResult.success(vo);
        }
    }
}
